import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from '@/views/Login.vue'
import HomeStudent from '@/views/HomeStudent'
import HomeTeachter from '@/views/HomeTeachter'

import noFound from '@/components/noFound'
import serverError from '@/components/serverError'

import lookExamCard from '@/views/studentPage/lookExamCard'
import stuSignup from '@/views/studentPage/stuSignup'

import roomsInfo from '@/views/manageClass/allRoomsInfo'
import useableRoomInfo from '@/views/manageClass/useableRoomInfo'

import manageExamInfo from '@/views/manageExam/addExam/manageExamInfo'

import manageMajorInfo from '@/views/manageExam/addExam/manageMajorInfo'
import manageStuInfo from '@/views/manageExam/addExam/manageStuInfo'

import checktPlaceInfo from '@/views/manageExam/modiExamInfo/manageExamPlace/checktPlaceInfo'

Vue.use(VueRouter)

const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push (location) {
  return originalPush.call(this, location).catch(err => err)
}

const router = new VueRouter({
  mode: 'hash',
  base: '/admin/', // 部署 tomcat 必须写
  routes: [
    {
      path: '/',
      redirect: '/Login'
    }, {
      path: '/Login',
      name: 'Login',
      component: Login
    }, {
      path: '/HomeStudent',
      name: 'HomeStudent',
      component: HomeStudent,
      children: [{
        path: '/studentPage/stuSignup',
        name: '考生报名',
        component: stuSignup,
        meta: {
          requireAuth: true
        }
      },
      {
        path: '/studentPage/lookExamCard',
        name: '查看准考证',
        component: lookExamCard,
        meta: {
          requireAuth: true
        }
      }]
    }, {
      path: '/HomeTeachter',
      name: 'HomeTeachter',
      component: HomeTeachter,
      children: [{
        path: '/manageClass/allRoomsInfo',
        name: '全部教室',
        component: roomsInfo,
        meta: {
          requireAuth: true
        }
      },
      {
        path: '/manageClass/useableRoomInfo',
        name: '可用教室',
        component: useableRoomInfo,
        meta: {
          requireAuth: true
        }
      },
      {
        path: '/manageExamInfo',
        name: '考试信息管理',
        component: manageExamInfo,
        meta: {
          requireAuth: true
        }
      },
      {
        path: '/manageMajorInfo',
        name: '专业信息管理',
        component: manageMajorInfo,
        meta: {
          requireAuth: true
        }
      },
      {
        path: '/manageStuInfo',
        name: '考生信息管理',
        component: manageStuInfo,
        meta: {
          requireAuth: true
        }
      },
      {
        path: '/checktPlaceInfo',
        name: '查看考场信息',
        component: checktPlaceInfo,
        meta: {
          requireAuth: true
        }
      }]
    },
    {
      path: '/noFound',
      name: 'noFound',
      component: noFound
    },
    {
      path: '/serverError',
      name: 'serverError',
      component: serverError
    }
  ]
})

router.beforeEach((to, from, next) => {
  if (to.path === '/Login') return next()
  // 获取token
  const tokenStr = window.sessionStorage.getItem('token')
  if (!tokenStr) return next('/Login')
  next()
})

export default router
